<?php

/**
 * @author kevin
 * @date 2016-01-16 14:00:00
 * @desc 首页广告
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
if(empty($_POST['st'])){
	api_err('0x008', 'param st is Missing');
}
//评价列表
if($_POST['st'] == 'list'){
	$goods_id = isset($_POST['goods_id']) ? intval($_POST['goods_id']) : 0;
	$page = isset($_POST['page']) ? abs(intval($_POST['page'])) : 1;
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	if(empty($goods_id)){
		api_err('0x008', 'param goods_id can not empty');
	}
	//0 商品评论 1文章评论
	$count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' .$GLOBALS['ecs']->table('comment').
           " WHERE id_value = '$goods_id' AND comment_type = 0 AND status = 1 AND parent_id = 0");
	$pager = get_pager('user.php', array('act' => 'collection_list'), $count, $page, $size);
	$GLOBALS['_CFG']['comments_number'] = $size;
	$data['list'] = assign_commentv1($goods_id, 0, $page);
	$data['page'] = get_app_pager($pager);
}
//用户ID检测
if(!isset($GLOBALS['uid'])){
	api_err('0x015', 'You have not logged on');
}
$user_id = $GLOBALS['uid'];
//添加评价
if($_POST['st'] == 'add'){
	$order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
    $comment = isset($_POST['comment'])?json_decode(base64_decode($_POST['comment']), true):'';
    if(empty($order_id) || empty($comment)){
        api_err('0x008', 'param order_id and comment can not empty');
    }
    foreach($comment as $value){
	    $goods_id = isset($value['goods_id']) ? intval($value['goods_id']) : 0;
        $comment_content = isset($value['comment_content'])?trim($value['comment_content']):'';
        if(empty($comment_content) || empty($goods_id)){
            continue;
            //api_err('0x008', 'param comment_content can not empty');
        }
        $sql = "SELECT o.order_id".
           " FROM " . $ecs->table('order_info'). " AS o, ".
           $ecs->table('order_goods') . " AS og ".
           " WHERE o.order_id = og.order_id".
           " AND o.user_id = '" . $user_id . "'".
           " AND o.order_id = '" . $order_id . "'".
           " AND og.goods_id = '" . $goods_id . "'".
           " AND (o.order_status = '" . OS_CONFIRMED . "' or o.order_status = '" . OS_SPLITED . "') ".
           " AND (o.pay_status = '" . PS_PAYED . "' OR o.pay_status = '" . PS_PAYING . "') ".
           " AND (o.shipping_status = '" . SS_SHIPPED . "' OR o.shipping_status = '" . SS_RECEIVED . "') ".
           " LIMIT 1";
        $tmp = $db->getOne($sql);
        if(empty($tmp)){
            continue;
            //api_err('0x008', 'The goods can not be evaluated');
        }
        $cmt = array(
            'user_id'           => $user_id,
            'comment_content'   => $comment_content,
            'goods_id'          => $goods_id,
            'rank'              => 3,
            'type'              => 0,
        );
        $res = add_commentv1($cmt);
        if(!$res){
            continue;
            //api_err('0x008', 'The comment add failed');
        }
        $sql = "update ".$ecs->table('order_goods')." set comment_id = ".$res." where goods_id = ".$goods_id;
        $db->query($sql);
    }
    $sql = "update ".$ecs->table('order_info')." set comment_flag = 1 where order_id = ".$order_id;
    $db->query($sql);
    $data = array();
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
//返回数据
data_back($data, '', RETURN_TYPE);

function add_commentv1($cmt)
{
    /* 评论是否需要审核 */
    $status = 1 - $GLOBALS['_CFG']['comment_check'];

    $user_id = empty($cmt['user_id']) ? 0 : $cmt['user_id'];
    $data = get_profile($user_id);
    $email = $data['email'];
    $user_name = $data['user_name'];
    $email = htmlspecialchars($email);
    $user_name = htmlspecialchars($user_name);

    /* 保存评论内容 */
    $sql = "INSERT INTO " .$GLOBALS['ecs']->table('comment') .
           "(comment_type, id_value, email, user_name, content, comment_rank, add_time, ip_address, status, parent_id, user_id) VALUES " .
           "('" .$cmt['type']. "', '" .$cmt['goods_id']. "', '$email', '$user_name', '" .$cmt['comment_content']."', '".$cmt['rank']."', ".gmtime().", '".real_ip()."', '$status', '0', '$user_id')";

    $result = $GLOBALS['db']->query($sql);
    $insert_id = $GLOBALS['db']->insert_id();
    clear_cache_files('comments_list.lbi');
    /*if ($status > 0)
    {
        add_feed($GLOBALS['db']->insert_id(), COMMENT_GOODS);
    }*/
    return $insert_id;
}


